package com.engineering.elink.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.common.ErrorType;
import com.engineering.elink.domain.Label;
import com.engineering.elink.newsdao.TagMapper;
import com.engineering.elink.service.LabelService;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.LabelMapper;

@Service
public class LabelServiceImpl implements LabelService {
	
	@Autowired
	private LabelMapper labelMapper;
	
	@Autowired
	private com.engineering.elink.databasedao.LabelMapper databaseCommonMapper; 
	
	@Autowired
	private com.engineering.elink.databasedao.LabelMapper libraryCommonMapper;
	
	@Autowired
	private com.engineering.elink.databasedao.LabelMapper postbarCommonMapper;
	
	@Autowired
	private TagMapper tagMapper;

	@Override
	public List<Integer> add(String labels,String userId,Integer type) {

		List<Label> labelList = new ArrayList<Label>();
		for (String labelName : labels.split(",")) {
			labelList.add(new Label(labelName,userId,type));
		}
		labelMapper.insertBatch(labelList);
		
		return labelMapper.select(labels,userId,labelList.size());
	}


	@Override
	public Map<String, Object> getLabel(Integer type, Integer count) {
		
		int typeValue=type.intValue();
		
		//类型(1资料2论文3课程4技能5问答6资迅)
		if(typeValue==1){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,databaseCommonMapper.selectLabel(null,count));
			
		}else if(typeValue==2){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,libraryCommonMapper.selectLabel(null,count));
			
		}else if(typeValue==3){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,labelMapper.selectLabel(0,null,count));
			
		}else if(typeValue==4){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,labelMapper.selectLabel(1,null,count));
			
		}else if(typeValue==5){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,postbarCommonMapper.selectLabel(null,count));
			
		}else if(typeValue==6){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,tagMapper.selectTag(null,count));
			
		}else{
			return ResJsonUtil.getMap(ErrorType.SEARCH_TYPE_ERROR);
		}
		
	}

	@Override
	public Map<String, Object> getKeyLabel(Integer type, String keyWord, Integer count) {
		
		int typeValue=type.intValue();
		
		if(StringUtils.isEmpty(keyWord)){
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
		}
		
		//类型(1资料2论文3课程4技能5问答6资迅)
		if(typeValue==1){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,databaseCommonMapper.selectLabel(keyWord,count));
			
		}else if(typeValue==2){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,libraryCommonMapper.selectLabel(keyWord,count));
			
		}else if(typeValue==3){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,labelMapper.selectLabel(0,keyWord,count));
			
		}else if(typeValue==4){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,labelMapper.selectLabel(1,keyWord,count));
			
		}else if(typeValue==5){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,postbarCommonMapper.selectLabel(keyWord,count));
			
		}else if(typeValue==6){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,tagMapper.selectTag(keyWord,count));
			
		}else{
			return ResJsonUtil.getMap(ErrorType.SEARCH_TYPE_ERROR);
		}
	}
	
}
